<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
    :append-to-body="true"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="0.8rem">
      <el-form-item label="托盘号" prop="tuopanbh">
        <el-input
          v-model.trim="form.tuopanbh"
          autocomplete="off"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item label="仓库名称">
        <el-input
          v-model.trim="form.cangkumc"
          autocomplete="off"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="form.zhuangtai">
          <el-radio :label="1">下架</el-radio>
          <el-radio :label="2">上架</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="绑定货位" v-if="this.title == '编辑'">
        <el-select
          v-model="form.huoweiid"
          placeholder="请选择"
          @change="selectHw"
          clearable
        >
          <el-option
            v-for="(item, index) in hwlist"
            :key="index"
            :label="item.huoweibh"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/userManagement'
  import { AddTuopan, queryHuoWeiList, UpdateTuopan } from '@/api/cangkujichu'
  export default {
    name: 'UserManagementEdit',
    data() {
      return {
        form: {
          tuopanbh: '',
          cangkumc: '',
          zhuangtai: 1,
          huoweiid: '',
        },
        rules: {
          tuopanbh: [
            { required: true, trigger: 'blur', message: '请输入托盘号' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        hwlist: [],
      }
    },
    async created() {
      const hwlist = await queryHuoWeiList()
      this.hwlist = hwlist.data
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '新增'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      selectHw(val) {
        let form = this.hwlist.find((item) => {
          return item.id == val
        })
        this.form.huoweibh = form.huoweibh
        this.form.ceng = form.ceng
        this.form.pai = form.pai
        this.form.lie = form.lie
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            var res = null
            if (this.title == '编辑') {
              res = await UpdateTuopan(this.form)
            } else {
              res = await AddTuopan(this.form)
            }
            this.$baseMessage(res.message, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
<style lang="scss" scoped>
  .el-select {
    width: 100%;
  }
</style>
